/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servlet.Test;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author boomar
 */
public class Calculatrice extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.close();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        try {
            String montant = request.getParameter("montant") != null ? request.getParameter("montant") : "";
            String monnaie = request.getParameter("monnaie") != null ? request.getParameter("monnaie") : "";
            double devise = 1.3924;
            double result = (!monnaie.equals("")
                    ? (monnaie.equals("etod")
                    ? (Double.parseDouble(montant) * devise)
                    : (Double.parseDouble(montant) / devise))
                    : 0);


            // TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Convertisseur Euro <-> Dollar</title>");
            out.println("</head>");
            out.println("<body><center>");
            out.println("<form method=\"POST\">");
            out.println("<input type=\"text\" name=\"montant\" value=\"" + result + "\" ><br/>");
            out.println("<input type=\"radio\" name=\"monnaie\" value=\"etod\" checked/> Euro to Dollar<br/>");
            out.println("<input type=\"radio\" name=\"monnaie\" value=\"dtoe\"/> Dollar to Euro<br/>");
            out.println("<input type=\"submit\" value=\"Convertir\">");
            out.println("</form>");
            out.println("</center></body>");
            out.println("</html>");
        } catch (NumberFormatException e) {
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Convertisseur Euro <-> Dollar</title>");
            out.println("</head>");
            out.println("<body><center>");
            out.println("<h2 style=\"color:red;\">Format incorrect</h2>");
            out.println("<form method=\"POST\">");
            out.println("<input type=\"text\" name=\"montant\" value=\"0\" ><br/>");
            out.println("<input type=\"radio\" name=\"monnaie\" value=\"etod\" checked/> Euro to Dollar<br/>");
            out.println("<input type=\"radio\" name=\"monnaie\" value=\"dtoe\"/> Dollar to Euro<br/>");
            out.println("<input type=\"submit\" value=\"Convertir\">");
            out.println("</form>");
            out.println("</center></body>");
            out.println("</html>");
        } finally {
            out.close();
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        try {
            String montant = request.getParameter("montant") != null ? request.getParameter("montant") : "";
            String monnaie = request.getParameter("monnaie") != null ? request.getParameter("monnaie") : "";
            double devise = 1.3924;
            double result = (!monnaie.equals("")
                    ? (monnaie.equals("etod")
                    ? (Double.parseDouble(montant) * devise)
                    : (Double.parseDouble(montant) / devise))
                    : 0);


            // TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Convertisseur Euro <-> Dollar</title>");
            out.println("</head>");
            out.println("<body><center>");
            out.println("<form method=\"POST\">");
            out.println("<input type=\"text\" name=\"montant\" value=\"" + result + "\" ><br/>");
            out.println("<input type=\"radio\" name=\"monnaie\" value=\"etod\" checked/> Euro to Dollar<br/>");
            out.println("<input type=\"radio\" name=\"monnaie\" value=\"dtoe\"/> Dollar to Euro<br/>");
            out.println("<input type=\"submit\" value=\"Convertir\">");
            out.println("</form>");
            out.println("</center></body>");
            out.println("</html>");
        } catch (NumberFormatException e) {
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Convertisseur Euro <-> Dollar</title>");
            out.println("</head>");
            out.println("<body><center>");
            out.println("<h2>Format incorrect</h2>");
            out.println("<form method=\"POST\">");
            out.println("<input type=\"text\" name=\"montant\" value=\"0\" ><br/>");
            out.println("<input type=\"radio\" name=\"monnaie\" value=\"etod\" checked/> Euro to Dollar<br/>");
            out.println("<input type=\"radio\" name=\"monnaie\" value=\"dtoe\"/> Dollar to Euro<br/>");
            out.println("<input type=\"submit\" value=\"Convertir\">");
            out.println("</form>");
            out.println("</center></body>");
            out.println("</html>");
        } finally {
            out.close();
        }
    }
}
